package com.fy.common.util;

import java.text.DecimalFormat;

/**
 * @author huangming
 * @date 2020/3/6 10:45
 */
public class NumberFormat {
    //两位整数
    public static final String TWO_INTEGER = "00";
    //两位小数
    public static final String TWO_DECIMAL = "0.##";
    //四位小数
    public static final String FOUR_DECIMAL = "0.####";

    /**
     * 格式化数字
     * @param number
     * @return
     */
    public static String formatNumber(long number, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return df.format(number);
    }

    /**
     * 格式化数字
     * @param number
     * @return
     */
    public static String formatNumber(double number, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return df.format(number);
    }

    /**
     * 阿拉伯数字转汉字（100以内）
     * @return
     */
    public static String numberToChines(int number) {
        String str ="";
        if(number < 0 || number > 99){
            str="不合法的数字";
        }else if(number >= 1 && number <= 10){
            str = cast(number);
        }else if(number >= 11 && number <= 19){
            int num = number % 10;
            str = "十"+cast(num);
        }else if(number >= 20 && number <= 99){
            int num1 = number / 10 % 10;
            int num2 = number % 10;
            str = cast(num1)+"十"+cast(num2);
        }
        return str;
    }

    public static String cast(int num){
        String str="";
        switch(num){
            case 0:
                str="";
                break;
            case 1:
                str="一";
                break;
            case 2:
                str="二";
                break;
            case 3:
                str="三";
                break;
            case 4:
                str="四";
                break;
            case 5:
                str="五";
                break;
            case 6:
                str="六";
                break;
            case 7:
                str="七";
                break;
            case 8:
                str="八";
                break;
            case 9:
                str="九";
                break;
            case 10:
                str="十";
                break;
        }
        return str;
    }
}
